<?php
namespace Web\Controller;
use Think\Controller;
use Web\Logic\MenuGetLogic;
class BaseController extends Controller {
/**
     * 管理员ID
     * @var $mAdminId
     * @access protected
     */
    protected $mAdminId     =  null;

    /**
     * 管理员名称
     * @var $mAdminName
     * @access protected
     */
    protected $mAdminName    =  null;

    /**
     * 管理员角色
     * @var $mAdminRole
     * @access protected
     */
    protected $mAdminRole    =  null;


    protected $mFunction_id = "";

    protected $mFunction_list = array();

   /**
     * 初始化函数 存Session，设置权限
     * @access protected
     * @author:zhouercong <2013-09-24>
     */
    protected function _initialize() {

        $actions = array('login', 'verify_c', 'register', 'forget', 'resetPwd');

        if (CONTROLLER_NAME == 'Index' && in_array(ACTION_NAME, $actions)) {
            return;
        }

        $session_admin_id = session(C('SESSION_ADMIN_ID'));
        $session_admin_name = session(C('SESSION_ADMIN_NAME'));
        $session_function_id = session('function_id');
        $session_flg = cookie("fazsw78awessdwsazswa");

        //判断是否登录, Id和Name都要存到Session中
        //if (false == $session_flg || false == $session_admin_id || false == $session_admin_name) {
        if (false == $session_admin_id || false == $session_admin_name) {
			if(IS_AJAX){
				header("SESSIONSTATUS: TIMEOUT");
				header("CONTEXTPATH: ".U('index/login'));
				exit;
			} else {
				$this->redirect("index/login");
			}
        } else {
            $this->mAdminId = $session_admin_id;
            $this->mAdminName = $session_admin_name;
            $this->mFunction_id = $session_function_id;

            $this->mFunction_id = $session_function_id;
            $this->mFunction_list = explode(",",$session_function_id);

            //模板变量赋值 登录用户名
            $this->assign("admin_id", $session_admin_id);
            //模板变量赋值 登录用户名
            $this->assign("admin_name", $session_admin_name);
            //模板变量赋值 用户角色
            $this->assign("role_name", session("rolename"));
            $this->assign("function_id", $session_function_id);
            $this->assign("action_name", ACTION_NAME);

            if(I('urlid') == null || I('urlid') == ""){
                $current_id =  $this->getCurrentIdByAct();

            }else{
                $current_id =  $this->getCurrentId(I('urlid'));

            }
            //实例化Logic
            $logic = new MenuGetLogic();
            //获取当前用户拥有的权限栏目
            $menuroot = session('menuroot');
            if (!CONTROLLER_NAME == 'Index'){
                $menuroot_list = explode(",",$menuroot);

                if(!in_array($current_id,$menuroot_list)){
                     $this->success("没有权限","index");

                }
            }
            //获取导航栏目
            //dump($menuroot);
            $dhData = $logic->dealWithMenu($menuroot,$current_id);

            $dhMenu = $dhData[0];
            $menu = $dhData[1];
            $str = json_encode($menu);
            $this->assign('menu',$str);
            $this->assign('dhMenu',$menu);

            
             $user_id = session(C('SESSION_ADMIN_ID'));
             $where['to_userid'] =  $user_id;
             $where['is_read'] = 0;
             $message_list = M('member_message')->where($where)->select();
             //echo M('member_message')->getLastSql();
             $this->assign('message_list',$message_list);
             //获取用户头像
             $img_url = M('member')->where(array('userid'=>$user_id))->getField("image_url");
             $img_url = $img_url?$img_url:"/Public/assets/images/users/user-0.jpg";
              $this->assign('user_img_url',$img_url);


        }
    }

     function getCurrentIdByAct(){

        $current_action_name = ACTION_NAME == 'edit' ? "index" : ACTION_NAME;
        $path =  strtolower(MODULE_NAME.'/'.CONTROLLER_NAME.'/'.$current_action_name);
        $where['lower(url)'] = array("like","$path%");
        $current_id = M('menu')->where($where)->Field('menuid,parentid')->find();
        $list[] = $current_id['menuid'];
        $list[] = $current_id['parentid'];
        $flg =true;
        $search_id = $current_id['parentid'];
        $iCount = 0;
        while($flg && $iCount <=5)
        {
            $iCount++;
            $sub_where['menuid'] = $search_id;

            $current_id = M('menu')->where($sub_where)->Field('menuid,parentid')->find();
            if($current_id['parentid'] != 0){

                 $search_id = $current_id['parentid'];
                 $list[] = $current_id['parentid'];
            }else{
                $flg = false;
                break;
            }
        }
        return $list;
    }

    function getCurrentId($urlid){

        $where['menuid'] =$urlid;
        $current_id = M('menu')->where($where)->Field('menuid,parentid')->find();
        $list[] = $current_id['menuid'];
        $list[] = $current_id['parentid'];
        $flg =true;
        $search_id = $current_id['parentid'];
        $iCount = 0;
        while($flg && $iCount <=5)
        {
            $iCount++;
            $sub_where['menuid'] = $search_id;

            $current_id = M('menu')->where($sub_where)->Field('menuid,parentid')->find();
            if($current_id['parentid'] != 0){

                 $search_id = $current_id['parentid'];
                 $list[] = $current_id['parentid'];
            }else{
                $flg = false;
                break;
            }
        }
        return $list;
    }


   


    /**
    * @desc 多维数组转换成一维数组
    * @access protected
    * @param $array 转换数组
    * @return $array
    * @author weihongwei <2013-10-16>
    */
    protected function arrayChang($array)
    {
        //一维数组
        static $chdata;
        //多维数组转换
        if(is_array($array))
        {
            foreach($array as $ar)
            {
                if(is_array($ar))
                {
                    $this->arrayChang($ar);
                }
                else
                {
                    $chdata[] =  $ar;
                }
            }
        }
        return $chdata;
    }

    /**
     * TODO 基础分页的相同代码封装，使前台的代码更少
     * @param $count 要分页的总记录数
     * @param int $pagesize 每页查询条数
     * @return \Think\Page
     */
    function getpage($count, $pagesize = 10) {
		$Page = new \Think\Page($count, $pagesize);
		$Page->lastSuffix = false;//最后一页不显示为总页数
        $Page->setConfig('header','<li class="disabled hwh-page-info"><a>共<em>%TOTAL_ROW%</em>条  <em>%NOW_PAGE%</em>/%TOTAL_PAGE%页</a></li>');
        $Page->setConfig('prev','上一页');
        $Page->setConfig('next','下一页');
        $Page->setConfig('last','末页');
        $Page->setConfig('first','首页');
        $Page->setConfig('theme','%HEADER% %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%');

    	return $Page;
    }

	function bootstrap_page_style($page_html){
		if ($page_html) {
			$page_show = str_replace('<div>','<nav><ul class="pagination">',$page_html);
            $page_show = str_replace('disabled hwh-page-info','page-link',$page_show);
			$page_show = str_replace('</div>','</ul></nav>',$page_show);
			$page_show = str_replace('<span class="current">','<li class="paginate_button page-item active"><a class="page-link">',$page_show);
			$page_show = str_replace('</span>','</a></li>',$page_show);
			$page_show = str_replace(array('<a class="num"','<a class="prev"','<a class="next"','<a class="end"','<a class="first"'),'<li class="paginate_button page-item"><a class="page-link"',$page_show);
			$page_show = str_replace('</a>','</a></li>',$page_show);
		}
		return $page_show;
	}


    //**************************************以下为一些公用的方法 方便共通设置**********************
    function getUserList(){

        $admin_role = M('role_info')->where(array('function_id'=>array('like',"%1000%")))->getField('roleid');

        $where['isforbidden'] = 0;
        $where['roleid'] = array('not in',$admin_role);
        $where['department_id'] = array('in',$this->department_list);
        $list = M('member')->where($where)->select();
        //echo M('member')->getLastSql();
        return $list;
    }

  
   



   


   

   

}